package com.zhangtai.modules.service.impl.salary;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangtai.modules.dao.SalaryBandDao;
import com.zhangtai.modules.entity.salary.SalaryBandEntity;
import com.zhangtai.modules.service.salary.SalaryBandService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;


@RestController
@Slf4j
public class SalaryBandServiceImpl extends ServiceImpl<SalaryBandDao, SalaryBandEntity> implements SalaryBandService {

    @Autowired
    private SalaryBandDao salaryBandDao;

    @Override
    public PageResult queryPage(@RequestParam(value = "orgSystemId", required = false) Long systemId,
                                @RequestParam(value = "orgLineId", required = false) Long lineId,
                                @RequestParam(value = "jobLevelId", required = false) Long jobLevelId,
                                @RequestParam(value = "orgIds", required = false) List<Long> orgIds,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
//        log.info("systemId:{}",systemId);
//        log.info("lineId:{}",lineId);
//        log.info("jobLevelId:{}",jobLevelId);
       QueryWrapper<SalaryBandEntity> queryWrapper =  new QueryWrapper<SalaryBandEntity>().eq(systemId!=null,"org_system_id",systemId)
                .eq(lineId!=null,"org_line_id",lineId)
                .eq(jobLevelId!=null,"job_level_id",jobLevelId)
                .eq("is_delete",0).orderByDesc("crate_time");
//               .in(!CollectionUtils.isEmpty(orgIds),"org_id",orgIds)
//               .eq(CollectionUtils.isEmpty(orgIds),"org_id",null);
        IPage<SalaryBandEntity> page = this.page(
                new Page<SalaryBandEntity>(pageNum,pageSize),
                queryWrapper
        );

        return new PageResult(page);
    }

    @Override
    public SalaryBandEntity getByIdE(Long id) {
            SalaryBandEntity one = this.getOne(new QueryWrapper<SalaryBandEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(SalaryBandEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(SalaryBandEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            SalaryBandEntity entity = new SalaryBandEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

    @Override
    public BigDecimal findByParam(@RequestParam("jobSystemId") Long jobSystemId, @RequestParam("lineId") Long lineId,
                                  @RequestParam("jobLevelId") Long jobLevelId) {
        List<SalaryBandEntity> bandEntityList = salaryBandDao.selectList(new QueryWrapper<SalaryBandEntity>().eq("org_system_id", jobSystemId).eq("org_line_id", lineId)
                .eq("job_level_id", jobLevelId));
        if (CollectionUtils.isEmpty(bandEntityList)){
           return null;
        }
        BigDecimal salaryLevel3 = bandEntityList.get(0).getSalaryLevel3();
        return salaryLevel3;
    }



}